﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Selectors.Controls
{
    public partial class Inspections : DialogEditBase
    {
        public Inspections()
        {
            InitializeComponent();
            BindLists();
        }

        private void Inspections_Load(object sender, EventArgs e)
        {
           
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInspectionResultList
        {
            get { return listSelectorInspectionResults.GetSelectedValueList<int>(); }
            set
            {
                listSelectorInspectionResults.SetSelectedValueList<int>(value);
                if (!listSelectorInspectionResults.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInspectionTypeList
        {
            get { return listSelectorInspectionType.GetSelectedValueList<int>(); }
            set
            {
                listSelectorInspectionType.SetSelectedValueList<int>(value);
                if (!listSelectorInspectionType.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInspectionNoteList
        {
            get { return listSelectorNotes.GetSelectedValueList<int>(); }
            set
            {
                listSelectorNotes.SetSelectedValueList<int>(value);
                if (!listSelectorNotes.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedVendorList
        {
            get { return listSelectorVendor.GetSelectedValueList<int>(); }
            set
            {
                listSelectorVendor.SetSelectedValueList<int>(value);
                if (!listSelectorVendor.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string SelectedInspectorList
        {
            get { return listSelectorInspector.GetSelectedValueList<int>(); }
            set
            {
                listSelectorInspector.SetSelectedValueList<int>(value);
                if (!listSelectorInspector.AllItemsChecked)
                    checkBoxXViewAll.Checked = false;
            }
        }

        [DefaultValue(true), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool? SelectedElectricalProblems
        {
            get 
            {
                if (checkBoxXViewAll.Checked)
                    return null;

                return checkBoxXViewAll.Checked; 
            }
            set 
            { 
                if (value != null)
                    checkBoxXViewAll.Checked = value.Value; 
            }
        }

        private void BindLists()
        {
            if (Program.LookupTableData != null)
            {
                listSelectorInspectionResults.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Name.ToString();
                listSelectorInspectionResults.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresResultTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_ResultTypeId.ToString();
                listSelectorInspectionResults.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresResultTypes;
                listSelectorInspectionResults.AddBlankOption<EntityLibrary.VendorInspVisitXmissionStructuresResultTypeEntity>();

                listSelectorInspectionType.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Name.ToString();
                listSelectorInspectionType.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresInspectionTypeEnumerations.Columns.Vendor_InspVisit_XmissionStructures_Inspection_TypeId.ToString();
                listSelectorInspectionType.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresInspectionType;
                listSelectorInspectionType.AddBlankOption<EntityLibrary.VendorInspVisitXmissionStructuresInspectionTypeEntity>();

                listSelectorNotes.DisplayMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Name.ToString();
                listSelectorNotes.ValueMember = EntityLibrary.VendorInspVisitXmissionStructuresNoteEnumerations.Columns.Vendor_InspVisit_XmissionStructures_NoteId.ToString();
                listSelectorNotes.DataSource = Program.LookupTableData.VendorInspVisitXmissionStructuresNotes;
                listSelectorNotes.AddBlankOption<EntityLibrary.VendorInspVisitXmissionStructuresNoteEntity>();

                //TODO: if Login type PPI need to find the appropriate vendors and contacts for the utility 
                listSelectorVendor.DisplayMember = EntityLibrary.VendorEnumerations.Columns.Name.ToString();
                listSelectorVendor.ValueMember = EntityLibrary.VendorEnumerations.Columns.VendorId.ToString();
                listSelectorVendor.DataSource = Program.LoginResult.Vendors;
                listSelectorVendor.AddBlankOption<EntityLibrary.VendorEntity>();
                
                listSelectorInspector.DisplayMember = "FriendlyName";
                listSelectorInspector.ValueMember = EntityLibrary.VendorContactEnumerations.Columns.Vendor_ContactId.ToString();
                listSelectorInspector.DataSource = Program.LoginResult.VendorContacts;
                listSelectorInspector.AddBlankOption<EntityLibrary.VendorContactEntity>();

                SetListsEnabled();
            }
        }

        private void checkBoxXViewAll_CheckedChanged(object sender, EventArgs e)
        {
            SetListsEnabled();
            checkBoxXElectricalProblems.Enabled = !checkBoxXViewAll.Checked;
        }

        private void SetListsEnabled()
        {
            bool _Enabled = !checkBoxXViewAll.Checked;

            foreach (Control _List in panelSelectors.Controls)
            {
                if ((_List as ListSelector) == null)
                    continue;

                (_List as ListSelector).Enabled = _Enabled;
            }
        }
    }
}
